<?
/*//////////////////////////////////////////////////////////////////////

Description:       Liberty Reserve Sample Store
Release Date:     Sep 20, 2007

Designed and Developed by Liberty Reserve development team.
Copyright (c) 2007 Liberty Reserve.
Website: http://www.libertyreserve.com
Contact email: tech@libertyreserve.com

This program is distributed in the hope that it will be useful but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

//////////////////////////////////////////////////////////////////////*/

define ('AEDITUUS_ROOT', '../');
require_once AEDITUUS_ROOT.'commun.php';

$db = db::getInstance();

setlocale(LC_ALL, 'fr_FR');

// Calcul la différence entre un Temps UTC et le temps locale
function tzdelta ( $iTime = 0 ) {
    if ( 0 == $iTime ) { $iTime = time(); }
    $ar = localtime ( $iTime );
    $ar[5] += 1900; $ar[4]++;
    $iTztime = gmmktime ( $ar[2], $ar[1], $ar[0], $ar[4], $ar[3], $ar[5], 0);
    return ( $iTztime - $iTime );
}


// You can recall any sent parameter using these sample codes:
// $_REQUEST["some_baggage_field"];
// $_REQUEST["lr_store"];
// $_REQUEST["lr_paidto"];
// $_REQUEST["lr_paidby"];
// and others..

// Building a string to be hashed
$str =
  $_REQUEST["lr_paidto"].":".
  $_REQUEST["lr_paidby"].":".
  stripslashes($_REQUEST["lr_store"]).":".
  $_REQUEST["lr_amnt"].":".
  $_REQUEST["lr_transfer"].":".
  $_REQUEST["lr_currency"].":".
  $conf_merchantSecurityWord;

//Calculating hash
$hash = strtoupper(bin2hex(mhash(MHASH_SHA256, $str)));

//Let's check that all parameters exist and match and that the hash
//string we computed matches the hash string that was sent by LR system.
if (isset($_REQUEST["lr_paidto"]) &&
    $_REQUEST["lr_paidto"] == strtoupper($conf_merchantAccountNumber) &&
    isset($_REQUEST["lr_store"]) &&
    stripslashes($_REQUEST["lr_store"]) == $conf_merchantStoreName &&
    isset($_REQUEST["lr_encrypted"]) &&
    $_REQUEST["lr_encrypted"] == $hash) {

// Response was validated and you can use this verified information to
// complete selling process.
// Enter any code here that you want to be executed after a successful
// payment.

// In our example we send email. The following code writes that the
// payment was good to the email body.
		
        @file_put_contents('printr.txt', print_r($_REQUEST, true));
        $send_mail = true;
		$currency = $_REQUEST['lr_currency'];
		
  $msgBody = "Payment was verified and is successful.\n\n";

    $locale_time = date('Y-m-d H:i:s');

    if (!empty($_REQUEST['order_id'])) {
        $db->query("UPDATE ".$cfg['prefix']."transaction SET date_paie_de=?, status_trans='enlivraison', num_trans_de=? WHERE id_trans=?", array($locale_time , $_REQUEST['lr_transfer'], $_REQUEST['order_id']));
		$db->query("UPDATE ".$cfg['prefix']."gainservices SET status_trans='enlivraison' WHERE id_trans=?", array($_REQUEST['order_id']));
		
        
    }
	if ($currency == "LRUSD")
		$db->query("UPDATE ".$cfg['prefix']."config SET value=value+? WHERE name='dispo_service_4_USD'", array($_REQUEST['eg_mont1_bis']));
	if ($currency == "LREUR") 
		$db->query("UPDATE ".$cfg['prefix']."config SET value=value+? WHERE name='dispo_service_4'", array($_REQUEST['eg_mont1_bis']));
}
else {

// This block is for the code in case that the payment verification has
// failed.
// In our example write the response to the body of the email we are
// going to send.
  $msgBody = "Invalid response. Sent hash didn't match the computed hash.\n";
}

// Let's get all the data sent by LR and add it to our email.
$msgBody .= "Received data\n";
$reqKeys = array_keys ($_REQUEST);
foreach($reqKeys as &$key) {
  $msgBody .= $key." = ".$_REQUEST[$key].(ereg("^lr_[a-z_]*$", $key) ? " (LR)" : "")."\n";
}
// Our example sends email with the result of verification and all the
// variables sent to this page.
if ($conf_merchantEmail != "") {
  mail($conf_merchantEmail, "LR SCI Status", $msgBody);
}

?>
